﻿using Management.Infrastructure.Interfaces.BOMManagement;
using MediatR;
using Management.Errorcode.ErrorCode;
using Management.API.Application.Controller.Management.Product;
using Management.API.DTOs.Management;
using AutoMapper;

namespace Management.API.Application.CommandHandler.Management.Product
{
    public class GetProductCommandHandler : IRequestHandler<GetProductCommand, ApiResult<IList<GetProductDTO>>>
    {
        private readonly IProductRepository iProductRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetProductCommandHandler> _logger;

        public GetProductCommandHandler(IProductRepository IProductRepository,
             IMapper mapper, ILogger<GetProductCommandHandler> logger)
        {
            iProductRepository = IProductRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<IList<GetProductDTO>>> Handle(GetProductCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iProductRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetProductDTO>>(useList);
                return ApiResult<IList<GetProductDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetProductCommandHandler");
                throw;
            }

        }

    }
}
